package com.pengtu.web.project;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.project.OwnerOrganization;
import com.pengtu.entity.project.ProjectInfo;
import com.pengtu.entity.project.SignUpContract;
import com.pengtu.service.project.ProjectManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Results({ @Result(name = BaseActionSupport.RELOAD, location = "projectinfo!save.action", type = "redirect") })
public class SignAction extends BaseActionSupport<SignUpContract> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ProjectManager projManager;

	private String id;
	private SignUpContract sign;
	private Page<SignUpContract> page = new Page<SignUpContract>(10);
	private String projectId;
	private String buildId;
	private OwnerOrganization ownerOrganization;

	public String getBuildId() {
		return buildId;
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public Page<SignUpContract> getPage() {
		return page;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Autowired
	public void setProjManager(ProjectManager projManager) {
		this.projManager = projManager;
	}

	@Override
	public SignUpContract getModel() {
		// TODO Auto-generated method stub
		return sign;
	}

	@Action("/project/sign/org/sign")
	public String orgList() {
		QueryFilter queryfilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		queryfilter.addFilter("projectId", projectId);
		if (!page.isOrderBySetted()) {
			page.orderBy("createDate");
			page.order(Page.ASC);
		}
		page = projManager.findPageSign(page, queryfilter);
		ProjectInfo pInfo = projManager.getProj("id", projectId);
		if(null!=pInfo){
			ownerOrganization = pInfo.getOrgCode();
		}
		return VIEW;
	}

	@Override
	public String list() throws Exception {
		if (StringUtils.isNotEmpty(projectId))
			sign = projManager.getSign("projectId", projectId);
		buildId = sign.getOrgCode();
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		projManager.saveOrupdateSign(sign);
		String mes = "";
		if (StringUtils.isNotEmpty(id)) {
			mes = "修改";
		} else {
			mes = "新增";
		}
		addActionMessage(mes + sign.getStartPegNum() + "至"
				+ sign.getEndPegNum() + "的合同记录！");
		return INPUT;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		addActionMessage(sign.getStartPegNum() + "至" + sign.getEndPegNum()
				+ "的合同被删除");
		projManager.deleteProj(id);
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			System.out.println(id);
			sign = projManager.getSign("id", id);
		} else {
			sign = new SignUpContract();
		}
	}

	public OwnerOrganization getOwnerOrganization() {
		return ownerOrganization;
	}

	public void setOwnerOrganization(OwnerOrganization ownerOrganization) {
		this.ownerOrganization = ownerOrganization;
	}

}
